package com.top.ruizhong.main.host

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ConvertUtils
import com.module.lemlin.base.BaseAbstractActivity
import com.top.ruizhong.R
import com.top.ruizhong.main.cate.MainCateFragment
import com.top.ruizhong.main.home.MainHomeFragment
import com.top.ruizhong.main.mine.MainMineFragment
import com.top.ruizhong.main.notice.MainMsgFragment
import com.top.ruizhong.main.video.CameraActivity
import com.top.ruizhong.main.video.MainVideoFragment
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.include_main_body.*


class MainActivity : BaseAbstractActivity() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MainActivity::class.java)
            activity.startActivity(intent)
        }
    }


    private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() {
        override fun onFragmentResumed(fm: FragmentManager, fragment: Fragment) {
            super.onFragmentResumed(fm, fragment)
            when (fragment) {
                is MainHomeFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (false))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), Color.TRANSPARENT)
                    drawerMainBody.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
                    val color: Int = Color.argb(fragment.alphaStatus, (255), (255), (255))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), color)
                    changeViewPagerBottomMargin(ConvertUtils.dp2px((51f)))
                }
                is MainRecomFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (false))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), Color.TRANSPARENT)
                    drawerMainBody.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
                }
                is MainCateFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (true))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), Color.TRANSPARENT)
                    drawerMainBody.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
                    changeViewPagerBottomMargin(ConvertUtils.dp2px((51f)))
                }
                is MainVideoFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (false))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), Color.TRANSPARENT)
                    drawerMainBody.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
                    changeViewPagerBottomMargin(ConvertUtils.dp2px((0f)))
                }
                is MainMsgFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (true))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), Color.TRANSPARENT)
                    drawerMainBody.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
                    changeViewPagerBottomMargin(ConvertUtils.dp2px((51f)))
                }
                is MainMineFragment -> {
                    BarUtils.setStatusBarLightMode(fragment.requireActivity(), (true))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), Color.TRANSPARENT)
                    val color: Int = Color.argb(fragment.alphaStatus, (255), (255), (255))
                    BarUtils.setStatusBarColor(fragment.requireActivity(), color)
                    drawerMainBody.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
                    changeViewPagerBottomMargin(ConvertUtils.dp2px((51f)))
                }
            }
        }
    }

    override val layoutResId: Int
        get() = R.layout.activity_main

    override fun stateBar(): StatusBar {
        return StatusBar(
            bottomColor = R.color.colorWhite
        )
    }

    override fun initView() {

        val fragments: MutableList<Fragment> = mutableListOf(
            MainRecomFragment.newInstance(),
            MainHomeFragment.newInstance(),
            MainCateFragment.newInstance(),
            MainMsgFragment.newInstance(),
            MainMineFragment.newInstance()
        )

        vpMainBody.apply {
            setScrollAnim(false)
        }.adapter = object : FragmentStatePagerAdapter(
            supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            override fun getItem(position: Int): Fragment = fragments[position]

            override fun getCount(): Int = fragments.size

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) = Unit
        }
        supportFragmentManager.registerFragmentLifecycleCallbacks(
            fragmentLifecycleCallbacks, (true)
        )

        rgMainBottomGroup.setOnCheckedChangeListener { group, checkedId ->
            if (checkedId == R.id.rbMainBottomHome) {
                if (rbMainBottomHome.isChecked)
                    changeBottomNavigationStyle(isStyle = true)
            } else {
                changeBottomNavigationStyle(isStyle = false)
            }
        }

        rbMainBottomHome.setOnClickListener {
            vpMainBody.currentItem = 0
        }

        rbMainBottomCate.setOnClickListener {
            vpMainBody.currentItem = 1
        }

        rbMainBottomVideo.setOnClickListener {
            vpMainBody.currentItem = 2
        }

        rbMainBottomMsg.setOnClickListener {
            vpMainBody.currentItem = 3
        }

        rbMainBottomMine.setOnClickListener {
            vpMainBody.currentItem = 4
        }

        ivMainVideoRecord.setOnClickListener {
//            XCamera2Activity.open(activity = this)
            CameraActivity.open(activity = this)
//            val intent = Intent(this, Camera2PreviewActivity::class.java)
//            startActivity(intent)
        }

    }

    override fun onBackPressed() {
        if (drawerMainBody.isDrawerOpen(GravityCompat.END)) {
            drawerMainBody.closeDrawers()
        } else {
            super.onBackPressed()
        }
    }

    fun onClick(view: View) {
        when (view.id) {
            R.id.ivMineSide -> {
                drawerMainBody.openDrawer(GravityCompat.END)
            }
            //视频/私人草稿箱
            R.id.tvMainSketchPrivate -> {
                MainSketchBoxActivity.open(activity = this, title = "私人草稿箱", type = 2)
            }
            R.id.tvMainSketchVideo -> {
                MainSketchBoxActivity.open(activity = this, title = "视频草稿箱", type = 1)
            }
            //设置
            R.id.tvMainSetting -> {
                MainSettingActivity.open(activity = this)
            }
            //设置
            R.id.tvMainFeedback -> {
                MainFeedbackActivity.open(activity = this)
            }
            //我的关注
            R.id.tvMainAttention -> {
                MainAttentionActivity.open(activity = this)
            }
            //数据总览
            R.id.tvMainOverview -> {
                MainOverviewActivity.open(activity = this)
            }
            //联系客服
            R.id.tvMainContact -> {
                MainRichTextActivity.open(activity = this, title = "联系客服")
            }
            //我的作品
            R.id.tvMainWorks -> {
                MainWorksActivity.open(activity = this)
            }
//            //商品管理
//            R.id.tvMainManage -> {
//                MainManageActivity.open(activity = this)
//            }
//            //我的订单
//            R.id.tvMainOrder -> {
//                MainOrderActivity.open(activity = this)
//            }
//            //兑换码
//            R.id.tvMainExchange -> {
//                MainExchangeActivity.open(activity = this)
//            }
//            //收益结算
//            R.id.tvMainSettlement -> {
//                MainSettlementActivity.open(activity = this)
//            }
//            //商家认证
//            R.id.tvMainMerchant -> {
//                MainMerchantActivity.open(activity = this)
//            }
        }
    }

    private fun changeBottomNavigationStyle(isStyle: Boolean) {
//        if (isStyle) {
//            rgMainBottomGroup.setBackgroundResource(R.drawable.ic_44749)
//        } else {
//            rgMainBottomGroup.setBackgroundResource(R.drawable.ic_49657)
//        }
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            BarUtils.setNavBarColor(
//                (this), ColorUtils.getColor(if (isStyle) R.color.FF_212121 else R.color.colorWhite)
//            )
//        }
    }

    private fun changeViewPagerBottomMargin(bottomMargin: Int = ConvertUtils.dp2px(0f)) {
//        (vpMainBody.layoutParams as FrameLayout.LayoutParams).let {
//            it.bottomMargin = bottomMargin
//            vpMainBody.layoutParams = it
//        }
    }

}